package com.jhhc.StormSurgeForecast.AstronomicalTide.Controller;

import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_STBPRP_B;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_STBPRP_BService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DTO.DataParam;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DataBO.AstronomictTide;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DataBO.AstronomictTideInfo;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DataBO.Departments;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_ST_ASTROTD_FService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.ReadUtil;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LYQ
 * 天文潮数据服务接口
 */
@Api(value="TideController",tags={"潮位数据服务"})
@RestController
@RequestMapping(value = "StormSurgeForecast/AstronomicalTide/Data", headers = "Accept=application/json")
public class DataAstronomictTideController {

    @Autowired
    private BDMS_ST_ASTROTD_FService fService;

    @Autowired
    private BDMS_ST_STBPRP_BService bService;


    /**
     * 获取天文潮预报部门信息列表
     */
    @RequestMapping(value = "/GetDepartments", method = RequestMethod.POST)
    public JsonResult getDepartments(@RequestBody DataParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getProjectId() != null) {
            List<Departments> departments = fService.getDepartments();
            if (departments != null && departments.size() > 0) {
                jsonResult.setData(departments);
            }else {
                jsonResult.setRemark("该预报部门信息为空");
            }
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 获取选定时间段内逐时天文潮
     */
    @RequestMapping(value = "/GetHourlyTwcTide", method = RequestMethod.POST)
    public JsonResult getHourlyTwcTide(@RequestBody DataParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getProjectId() != null && param.getStationCodes() != null && param.getStationCodes().size() > 0
        && param.getStartTime() != null && !"".equals(param.getStartTime()) && param.getEndTime() != null
        && !"".equals(param.getEndTime()) && param.getDepartmentId() != null && !"".equals(param.getDepartmentId())) {
            List<AstronomictTide> astronomictTides = fService.getHourlyTwcTide(param.getStartTime(),
                    param.getEndTime(),param.getStationCodes(),param.getDepartmentId(),
                    param.isGetHLTide(),param.getProjectId());
            if (astronomictTides != null && astronomictTides.size() > 0) {
                double max = 0, min = 0;
                List<BDMS_ST_STBPRP_B> bList = bService.selectByPid(param.getProjectId(),param.getStationCodes());
                for (AstronomictTide tide : astronomictTides) {
                    if (bList != null && bList.size() > 0) {
                        List<BDMS_ST_STBPRP_B> bs = bList.stream().filter(x -> x.getStcd().equals(tide.getStationCode()))
                                .filter(x -> x.getPid().equals(param.getProjectId()))
                                .collect(Collectors.toList());
                        if (bs.size() > 0) {
                            tide.setStationName(bs.get(0).getStnm());
                            tide.setTitle(bs.get(0).getStnm() + "（" + tide.getStationCode() + "）天文潮预报过程图");
                        }
                    }else {
                        tide.setTitle("（" + tide.getStationCode() + "）天文潮预报过程图");
                    }
                    for (AstronomictTideInfo baseData : tide.getBaseDatas()) {
                        if ("twc".equals(baseData.getSenserType())) {
                            if (baseData.getMaxData().getTdz() != null && !"".equals(baseData.getMaxData().getTdz())) {
                                max = Double.parseDouble(baseData.getMaxData().getTdz()) * 0.3 +
                                        Double.parseDouble(baseData.getMaxData().getTdz());
                            }
                            if (baseData.getMinData().getTdz() != null && !"".equals(baseData.getMinData().getTdz())) {
                                min = Double.parseDouble(baseData.getMinData().getTdz()) -
                                        Math.abs(Double.parseDouble(baseData.getMinData().getTdz()) * 0.3);
                            }
                        }
                    }
                    tide.setAxisY((int)Math.ceil(max) + "," + (int)Math.floor(min));
                }
                jsonResult.setData(astronomictTides);
            }else {
                jsonResult.setRemark("该时间段天文潮信息为空");
            }
        }else {
            jsonResult.setRemark("参数不完整");
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 获取选定时间段内天文高低潮
     */
    @RequestMapping(value = "/GetHLTwcTide", method = RequestMethod.POST)
    public JsonResult getHLTwcTide(@RequestBody DataParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getProjectId() != null && param.getStationCodes() != null && param.getStationCodes().size() > 0
                && param.getStartTime() != null && !"".equals(param.getStartTime()) && param.getEndTime() != null
                && !"".equals(param.getEndTime()) && param.getDepartmentId() != null && !"".equals(param.getDepartmentId())) {

            List<AstronomictTide> astronomictTides = fService.getHLTwcTide(param.getStartTime(),
                    param.getEndTime(),param.getStationCodes(),param.getDepartmentId(),param.getProjectId());
            if (astronomictTides != null && astronomictTides.size() > 0) {
                double max = 0;
                double min = 0;
                List<BDMS_ST_STBPRP_B> bList = bService.selectByPid(param.getProjectId(),param.getStationCodes());
                for (AstronomictTide tide : astronomictTides) {
                    if (bList != null && bList.size() > 0) {
                        List<BDMS_ST_STBPRP_B> bs = bList.stream().filter(x -> x.getStcd().equals(tide.getStationCode()))
                                .filter(x -> x.getPid().equals(param.getProjectId()))
                                .collect(Collectors.toList());
                        if (bs.size() > 0) {
                            tide.setStationName(bs.get(0).getStnm());
                            tide.setTitle(bs.get(0).getStnm() + "（" + tide.getStationCode() + "）天文高低潮预报过程图");
                        }
                    }else {
                        tide.setTitle("（" + tide.getStationCode() + "）天文高低潮预报过程图");
                    }
                    for (AstronomictTideInfo baseData : tide.getBaseDatas()) {
                        if ("twc".equals(baseData.getSenserType())) {
                            if (baseData.getMaxData().getTdz() != null && !"".equals(baseData.getMaxData().getTdz())) {
                                max = Double.parseDouble(baseData.getMaxData().getTdz()) * 0.2 +
                                        Double.parseDouble(baseData.getMaxData().getTdz());
                            }
                            if (baseData.getMinData().getTdz() != null && !"".equals(baseData.getMinData().getTdz())) {
                                min = Double.parseDouble(baseData.getMinData().getTdz()) -
                                        Double.parseDouble(baseData.getMinData().getTdz()) * 0.2;
                            }
                        }
                    }
                    tide.setAxisY((int)Math.ceil(max) + "," + (int)Math.floor(min));
                }
                jsonResult.setData(astronomictTides);
            }else {
                jsonResult.setRemark("该时间段天文潮信息为空");
            }
        }else {
            jsonResult.setRemark("参数不完整");
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    /**
     * 获取选定年份所有天文潮数据
     */
    @RequestMapping(value = "/GetTwcTideByYear", method = RequestMethod.POST)
    public JsonResult getTwcTideByYear(@RequestBody DataParam param){
        JsonResult jsonResult = new JsonResult();
        if (param.getProjectId() != null && param.getStationCodes() != null && !param.getStationCodes().isEmpty()
                &&  param.getYear() != null && !param.getYear().isEmpty() && param.getDepartmentId() != null
                && !param.getDepartmentId().isEmpty()) {

            List<AstronomictTide> astronomictTides = fService.getTwcTideByYear(param.getYear(),
                    param.getStationCodes(),param.getDepartmentId(),param.getProjectId());
            if (astronomictTides != null && !astronomictTides.isEmpty()) {
                double max = 0;
                double min = 0;
                List<BDMS_ST_STBPRP_B> bList = bService.selectByPid(param.getProjectId(),param.getStationCodes());
                for (AstronomictTide tide : astronomictTides) {
                    if (bList != null && !bList.isEmpty()) {
                        List<BDMS_ST_STBPRP_B> bs = bList.stream().filter(x -> x.getStcd().equals(tide.getStationCode()))
                                .filter(x -> x.getPid().equals(param.getProjectId()))
                                .collect(Collectors.toList());
                        if (!bs.isEmpty()) {
                            tide.setStationName(bs.get(0).getStnm());
                            tide.setTitle(bs.get(0).getStnm() + "（" + tide.getStationCode() + "）年天文潮预报过程图");
                        }
                    }else {
                        tide.setTitle("（" + tide.getStationCode() + "）年天文潮预报过程图");
                    }
                    for (AstronomictTideInfo baseData : tide.getBaseDatas()) {
                        if ("twc".equals(baseData.getSenserType())) {
                            if (baseData.getMaxData().getTdz() != null && !baseData.getMaxData().getTdz().isEmpty()) {
                                max = Double.parseDouble(baseData.getMaxData().getTdz()) * 0.2 +
                                        Double.parseDouble(baseData.getMaxData().getTdz());
                            }
                            if (baseData.getMinData().getTdz() != null && !baseData.getMinData().getTdz().isEmpty()) {
                                min = Double.parseDouble(baseData.getMinData().getTdz()) -
                                        Double.parseDouble(baseData.getMinData().getTdz()) * 0.2;
                            }
                        }
                    }
                    tide.setAxisY((int)Math.ceil(max) + "," + (int)Math.floor(min));
                }
                jsonResult.setData(astronomictTides);
            }else {
                jsonResult.setRemark("该时间段天文潮信息为空");
            }
        }else {
            jsonResult.setRemark("参数不完整");
        }
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

}
